package cn.nexd.app.config.ui.activity;

import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.OnClick;
import cn.nexd.app.config.R;
import cn.nexd.app.config.adapter.CheckedMallAdapter;
import cn.nexd.app.config.model.Mall;
import cn.nexd.app.config.presenter.ConfigPresenter;
import cn.nexd.app.config.ui.iView.IConfigActivityView;
import cn.nexd.app.config.util.ProgressDialogUtils;

/**
 * Created by wangxu on 16/7/19.
 */
public class ConfigActivity extends BaseActivity<ConfigPresenter> implements IConfigActivityView {

    @Bind(R.id.email)
    AutoCompleteTextView email;

    @Bind(R.id.password)
    EditText password;

    @Bind(R.id.mode_wifi)
    CheckBox wifi;

    @Bind(R.id.mode_bluetooth)
    CheckBox bluetooth;

    @Bind(R.id.mode_map)
    CheckBox map;

    @Bind(R.id.mode_route)
    CheckBox routePlan;

    @Bind(R.id.mall_list)
    ListView mallList;


    public static CheckedMallAdapter adapter;


    public static ArrayList<Mall> selectModes;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_config;
    }

    @Override
    protected void initPresenter() {
        presenter = new ConfigPresenter(this,this);
        presenter.init();
    }

    @Override
    public void initView() {
        selectModes = new ArrayList<>();
        adapter = new CheckedMallAdapter(this);
        mallList.setAdapter(adapter);
        presenter.getBuildingsByHttp();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(ConfigActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case ConfigPresenter.REQUEST_CODE:
                // 像集合中添加数据
                if (resultCode == RESULT_OK) {
                    selectModes = data.getParcelableArrayListExtra("malls");
                    adapter.addMalls(selectModes);
                }
                break;

            default:

                break;
        }
    }

    @OnClick(R.id.select_mall)
    public void selectMall() {
        // 网络请求延迟,导致传递输出为空时,禁止进入选择页面.
        presenter.selectMall();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case R.id.action_compose:
                presenter.setWifiChecked(wifi.isChecked());
                presenter.setBluetoothChecked(bluetooth.isChecked());
                presenter.setMapChecked(map.isChecked());
                presenter.setRoutePlanChecked(routePlan.isChecked());
                presenter.commit(email.getText().toString(), password.getText().toString());
                break;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public void setEmailError(String msg) {
        email.setError(msg);
    }

    @Override
    public void setPasswordError(String msg) {
        password.setError(msg);
    }

    @Override
    public void showProgress() {
        ProgressDialogUtils.showProgressDialog(this, getString(R.string.msg_upload_uploading), "", false);
    }

    @Override
    public void hideProgress() {
        ProgressDialogUtils.dismissProgressDialog(this);
    }

}
